package test.openai.tool.webhook;

import com.alibaba.fastjson.JSONObject;
import com.liuqi.openai.core.chat.Function;
import com.liuqi.openai.core.chat.Parameters;
import com.liuqi.openai.core.chat.Tool;
import com.liuqi.openai.tool.webhook.WebHook;
import com.liuqi.openai.tool.webhook.WebHookTool;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.liuqi.openai.util.ValidationUtil.ensureNotBlank;
import static com.liuqi.openai.util.ValidationUtil.ensureNotNull;

/**
 * @author liuqi
 * @date 2025/7/20
 **/
public class WebHookToolParse {

    public static List<WebHookTool> parse(String filepath) throws IOException {
        byte[] bytes = Files.readAllBytes(new File(filepath).toPath());
        List<WebHookJsonSchema> webHookJsonSchemas = JSONObject.parseArray(new String(bytes), WebHookJsonSchema.class);

        return parse(webHookJsonSchemas);
    }

    public static List<WebHookTool> parse(List<WebHookJsonSchema> webHookJsonSchemas) {
        List<WebHookTool> webHookTools = new ArrayList<>();

        for (WebHookJsonSchema schema : webHookJsonSchemas) {
            String groupName = ensureNotBlank(schema.getGroupName(), "parse [web hook schema] error, group_name");
            List<String> servers = ensureNotNull(schema.getServers(), "parse [web hook schema] error, servers");
            Map<String, String> headers = ensureNotNull(schema.getHeaders(), "parse [web hook schema] error, headers");
            String contentType = ensureNotBlank(schema.getContentType(), "parse [web hook schema] error, contentType");

            List<WebHookJsonSchema.Paths> paths = schema.getPaths();
            if (paths == null || paths.isEmpty()) {
                throw new NullPointerException(String.format("parse [web hook schema] error, groupName: [%s], paths is empty", groupName));
            }

            for (WebHookJsonSchema.Paths path : paths) {
                WebHook webHook = WebHook.builder()
                        .servers(servers)
                        .headers(headers)
                        .contentType(contentType)
                        .path(path.getPath())
                        .build();

                String toolName = WebHookJsonSchema.toolName(groupName, path.getName());
                WebHookTool webHookTool = WebHookTool.builder()
                        .toolName(toolName)
                        .webHook(webHook)
                        .chatTool(
                                Tool.builder()
                                        .type("function")
                                        .function(
                                                Function.builder()
                                                        .name(toolName)
                                                        .description(path.getDescription())
                                                        .parameters(
                                                                Parameters.builder()
                                                                        .properties(path.getParameters().getProperties())
                                                                        .required(path.getParameters().getRequired())
                                                                        .type("object")
                                                                        .build()
                                                        )
                                                        .build()
                                        )
                                        .build()
                        )
                        .build();

                webHookTools.add(webHookTool);
            }
        }

        return webHookTools;
    }

    public static void main(String[] args) throws IOException {
        List<WebHookTool> webHookTools = parse("C:\\liuqi-ai-project\\openai-sdk\\src\\test\\resources\\webhook.json");

        System.out.println();
    }

}
